﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using mscook.core.services;
using mscook.core;
using mscook.core.domain;
using mscook.mvc.core;
using mscook.mvc.Areas.weblogin.Models;
using System.Linq.Expressions;
namespace mscook.mvc.Areas.weblogin.Controllers
{
    public class HotelController : BaseController
    {
        protected IHotelService iHotelService;
        private IImageSettingFactory iImageSettingFactory;
        private ILoginUserService iLoginUserService;
        private readonly string SectionName = "Hotel";
        public HotelController(IHotelService IHotelService, IImageSettingFactory IImageSettingFactory, ILoginUserService iLoginUserService)
            : base(iLoginUserService)
        {
            this.iLoginUserService = iLoginUserService;
            this.iHotelService = IHotelService;
            this.iImageSettingFactory = IImageSettingFactory;
        }
        public ActionResult Index(string nowPageNum,string hotelName="",string phone="",string per = "")
        {

            Expression<Func<Hotel, bool>> select_expression = o => (string.IsNullOrEmpty(hotelName)||o.Name.Contains(hotelName))
                &&(string.IsNullOrEmpty(phone)||o.PhoneNum.Contains(phone));

            PageBreakHelper pageHelper = new PageBreakHelper();
            pageHelper.Init(nowPageNum, per);
            long total = 0;
            IList<Hotel> HotelList = iHotelService.SelectPageListByExpressions(pageHelper.Start, pageHelper.PerValue, out total, select_expression);
            pageHelper.SetViewDataKeys(ViewData, ViewBag, total);

            InitNavBar();

            ViewData["hotelName"] = hotelName;
            ViewData["phone"] = phone;
            ViewData["nowPageNum"] = nowPageNum;
            ViewData["per"] = per;

            return View(HotelList);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Add()
        {
            InitNavBar();
            return View();
        }
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult Add(Hotel hotel, string HotelTypeId,bool IsTop=false)
        {
            if (ModelState.IsValid)
            {
                if (SaveUploadImage(hotel))
                {
                    hotel.EntityIndex = IsTop ? 1 : 0;
                    SaveResourceType(hotel, HotelTypeId);
                    iHotelService.SaveOrUpdate(hotel);                  
                    return RedirectToIndex();
                }
            }
            return View();
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Edit(string id)
        {
           
            Hotel hotel = iHotelService.Get(id);
            if (hotel != null)
            {
                HotelModel hotelModel = new HotelModel() { Hotel=hotel};
             
                hotelModel.ResourceTypes = GetResourceType(hotel.Id);

                InitNavBar();

                return View(hotelModel);
            }
            return RedirectToIndex();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult Edit(string id, string HotelTypeId, bool IsTop = false)
        {
           // Hotel hotel = iHotelService.Get(id);
            HotelModel hotelModel = new HotelModel();
            hotelModel.Hotel = iHotelService.Get(id);
            if (ModelState.IsValid)
            {
                hotelModel.Hotel.EntityIndex = IsTop ? 1 : 0;
                SaveResourceType(hotelModel.Hotel, HotelTypeId);
                SaveUploadImage(hotelModel.Hotel);
                UpdateModel(hotelModel);
                iHotelService.SaveOrUpdate(hotelModel.Hotel);
                return RedirectToIndex();
            }
            return View(hotelModel);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Delete(string id)
        {
            iHotelService.Delete(id);
            return RedirectToIndex();
        }


        public ActionResult HotelModal(string nameInput, string idInput,string hotelName, string num = "", string linkBtn = "")
        {
            PageBreakHelper pageHelper = new PageBreakHelper();
            pageHelper.Init(num, "3");
            long total = 0;
            IList<Hotel> HotelList = iHotelService.SelectPageList(pageHelper.Start, pageHelper.PerValue, out total, hotelName);
            pageHelper.SetViewDataKeys(ViewData, ViewBag, total);
            ViewData["nameInput"] = nameInput;
            ViewData["idInput"] = idInput;
            ViewData["linkBtn"] = linkBtn;
            return View(HotelList);
        }

        private bool SaveUploadImage(Hotel Hotel)
        {
            if (Request.Files.Count > 0)
            {
                HttpPostedFileBase file = Request.Files.Get(0);
                if (file.ContentLength != 0)
                {
                    Hotel.ImageName = iImageSettingFactory.CreateBatchSizedImage(SectionName, file.FileName, file.InputStream);
                }
                SaveBottomImage(Hotel);
                return true;
            }
            return false;
        }

        private void SaveBottomImage(Hotel Hotel)
        {
            if (Request.Files.Count == 1)
            {
                //没有上传BottomImage，直接return
                return;
            }

            string SectionName = "BottomHotel";
            HttpPostedFileBase file = Request.Files.Get(1);
            if(file != null && file.ContentLength != 0)
            {
                Hotel.BottomImagePath = iImageSettingFactory.CreateBatchSizedImage(SectionName, file.FileName, file.InputStream);
            }
        }

    }
}
